package com.higgins.eventtracking.client.health.overview;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.TabBar;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.higgins.eventtracking.client.health.Fundamentals;
import com.higgins.eventtracking.client.health.components.GenericStatsTabParent;
import com.higgins.eventtracking.client.health.components.GenericTabInterface;

public class AchievementTab extends VerticalPanel implements GenericStatsTabParent, GenericTabInterface 		{
	private boolean firstRun = true;
	private TabPanel tabPanel = new TabPanel();
	private Fundamentals parentTab = null;
	private List<AchievementSubTab> listOfTabs = new ArrayList<AchievementSubTab>();
	
	private String relationship = "Relationship";
	private String legalCareer = "Legal Career";
	private String buildForFuture = "Build For The Future";
	private String houseKeeping = "House Keeping";
	private String currentCareer = "Current Career";
	private String relax = "Relax";
	private String social = "Social";
	
//	private Integer tab1 = 0;
//	private Integer tab2 = 0;
//	private Integer tab3 = 0;
//	private Integer tab4 = 0;
//	private Integer tab5 = 0;
//	private Integer tab6 = 0;
//	private Integer tab7 = 0;
		
	private AchievementSubTab houseKeepingTab = new AchievementSubTab(this, houseKeeping);
	private AchievementSubTab buildForFutureTab = new AchievementSubTab(this, buildForFuture);
	private AchievementSubTab lawCareerTab = new AchievementSubTab(this, legalCareer);
	private AchievementSubTab currentCareerTab = new AchievementSubTab(this, currentCareer);
	private AchievementSubTab relaxTab = new AchievementSubTab(this, relax);
	private AchievementSubTab relationshipTab = new AchievementSubTab(this, relationship);
	private AchievementSubTab socialTab = new AchievementSubTab(this, social);
	
	private List<Integer> scores = new ArrayList<Integer>();

	private String tabTitle = null;
	private Integer tabIndex = null;

	
	public AchievementTab(Fundamentals parentTab)	{
		this.parentTab = parentTab;
		scores.add(0);
		scores.add(0);
		scores.add(0);
		scores.add(0);
		scores.add(0);
		scores.add(0);
		scores.add(0);
		setWidth("800px");

		
	}
	public void update()	{
		if (firstRun)	
			initialise();
	}
	public void initialise()	{
		populateTabPanel();
		addTabs();
		firstRun = false;
	}
	public void populateTabPanel()	{
		houseKeepingTab.update();
		buildForFutureTab.update();
		currentCareerTab.update();
		lawCareerTab.update();
		relaxTab.update();
		relationshipTab.update();
		socialTab.update();
		add(tabPanel);
		tabPanel.addSelectionHandler(createSelectionHandler());
	}
	public SelectionHandler<Integer> createSelectionHandler()	{
		SelectionHandler<Integer> selectionHandler = new SelectionHandler<Integer>() {
	        @Override
	        public void onSelection(SelectionEvent<Integer> integerSelectionEvent) {
	        	AchievementSubTab subTab = listOfTabs.get(integerSelectionEvent.getSelectedItem());
	        	subTab.update();
	        	
//	            if (integerSelectionEvent.getSelectedItem() == 0)	{
//	            	houseKeepingTab.update();
//	            }
//	            if (integerSelectionEvent.getSelectedItem() == 1)	{
//	            	buildForFutureTab.update();
//	            }
//	            if (integerSelectionEvent.getSelectedItem() == 2)	{
//	            	lawCareerTab.update();
//	            }
//	            if (integerSelectionEvent.getSelectedItem() == 3)	{
//	            	currentCareerTab.update();
//	            }
//	            if (integerSelectionEvent.getSelectedItem() == 4)	{
//	            	relaxTab.update();
//	            }	            
//	            if (integerSelectionEvent.getSelectedItem() == 5)	{
//	            	relationshipTab.update();
//	            }
//	            if (integerSelectionEvent.getSelectedItem() == 6)	{
//	            	socialTab.update();
//	            }
	        }
		};
		return selectionHandler;
	}
	public void setSubHeaderCount(Integer subTabIndex, int count)	{
		scores.set(subTabIndex, new Integer(count));
		
//		if (header.equals(relationship))	{
//			tab1 = count;
//		} else if (header.equals(legalCareer))	{
//			tab2 = count;
//		} else if (header.equals(buildForFuture))	{
//			tab3 = count;
//		}  else if (header.equals(houseKeeping))	{
//			tab4 = count;
//		}  else if (header.equals(currentCareer))	{
//			tab5 = count;
//		}  else if (header.equals(relax))	{
//			tab6 = count;
//		}  else if (header.equals(social))	{
//			tab7 = count;
//		} 
		passBackToParent();
	}
	private void passBackToParent()	{
//		if (tab1!=null && tab2!=null && tab3!=null && tab4!=null && tab5!=null && tab6!=null && tab7!=null)	{
//			addTabs();
			int max = max();
			Double total = new Double(scores.get(0) + scores.get(1) + scores.get(2) + scores.get(3) + scores.get(4) + scores.get(5) + scores.get(6));
			Double average = total/7.0;
			Double aveDivByMax = (average*100.0)/max;
			parentTab.updateTabTitle(tabIndex, tabTitle + "(" + aveDivByMax.intValue() + " % )", false);
//		}
	}
	private void addTabs()	{
		int subTabIndex = 0;
		
		houseKeepingTab.setTabIndex(subTabIndex++);
		houseKeepingTab.setTabTitle(houseKeeping);
		listOfTabs.add(houseKeepingTab);		
		tabPanel.add(houseKeepingTab, houseKeepingTab.getTitle());
		tabPanel.selectTab(0);

		buildForFutureTab.setTabIndex(subTabIndex++);
		buildForFutureTab.setTabTitle(buildForFuture);
		listOfTabs.add(buildForFutureTab);		
		tabPanel.add(buildForFutureTab, buildForFutureTab.getTitle());
		
		lawCareerTab.setTabIndex(subTabIndex++);
		lawCareerTab.setTabTitle(legalCareer);
		listOfTabs.add(lawCareerTab);		
		tabPanel.add(lawCareerTab, lawCareerTab.getTitle());

		currentCareerTab.setTabIndex(subTabIndex++);
		currentCareerTab.setTabTitle(currentCareer);
		listOfTabs.add(currentCareerTab);		
		tabPanel.add(currentCareerTab, currentCareerTab.getTitle());				

		relaxTab.setTabIndex(subTabIndex++);
		relaxTab.setTabTitle(relax);
		listOfTabs.add(relaxTab);		
		tabPanel.add(relaxTab, relaxTab.getTitle());
		
		relationshipTab.setTabIndex(subTabIndex++);
		relationshipTab.setTabTitle(relationship);
		listOfTabs.add(relationshipTab);		
		tabPanel.add(relationshipTab, relationshipTab.getTitle());
		
		socialTab.setTabIndex(subTabIndex++);
		socialTab.setTabTitle(social);
		listOfTabs.add(socialTab);		
		tabPanel.add(socialTab, socialTab.getTitle());
	}
	private int max()	{
		int max = Math.max(scores.get(0), scores.get(1));
		max = Math.max(max, scores.get(2));
		max = Math.max(max, scores.get(3));
		max = Math.max(max, scores.get(4));
		max = Math.max(max, scores.get(5));
		max = Math.max(max, scores.get(6));
		return max!=0?max:1;
	}
	@Override
	public void updateTabTitle(Integer tabIndex, String title, Boolean alert) {
		TabBar tabBar = tabPanel.getTabBar();
		Integer score = scores.get(tabIndex);
		tabBar.setTabHTML(tabIndex, (title + "(" + (score!=null?score:0) + ")"));
	}
	@Override
	public String getTabTitle() {
		return tabTitle;
	}

	@Override
	public void setTabTitle(String tabTitle) {
		this.tabTitle = tabTitle;
	}

	@Override
	public Integer getTabIndex() {
		return tabIndex;
	}

	@Override
	public void setTabIndex(Integer tabIndex) {
		this.tabIndex = tabIndex;
	}
}
